﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public  class OUserDAL
    {
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Add(OUserInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_Add", obj.UserName, obj.PassWord, obj.RealName, obj.UserType, obj.IsSuperAdmin, obj.RoleId, obj.Mobile, obj.EMail, obj.Department,obj.PartsEmail,obj.SrvActUserId);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand))>0;
        }
        /// <summary>
        /// 检测用户名是否存在
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static bool CheckUserName(string UserName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_CheckUserName", UserName);
            //大于0表示存在
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand)) > 0;

        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static bool Delete(int UserId)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("OUser_Delete", UserId);
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
             
        }
        /// <summary>
        /// 获取用户信息根据ID
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static IDataReader GetById(int UserId)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_GetById", UserId);
            return DB.ExecuteReader(dbCommand);
	    }

        public static IDataReader GetByUserName(string UserName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_GetByUserName", UserName);
            return DB.ExecuteReader(dbCommand);
        }

        public static DataTable GetPagination(string RealName, string RoleId, int UserType, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_GetPagination");
            DB.AddInParameter(dbCommand, "RealName", DbType.String,RealName);
            DB.AddInParameter(dbCommand, "RoleId", DbType.String, RoleId);
            DB.AddInParameter(dbCommand, "UserType", DbType.Int32,UserType);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        /// <summary>
        /// 更新
        /// </summary>      
        public static bool Update(OUserInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_Update", obj.UserId, obj.UserName, obj.RealName, obj.IsSuperAdmin, obj.RoleId, obj.Mobile,obj.EMail,obj.Department,obj.PartsEmail,obj.SrvActUserId,obj.ClaimAmount);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <param name="LoginIP"></param>
        public static IDataReader UserLogin(string UserName, string PassWord, string LoginIP)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_UserLogin", UserName, PassWord, LoginIP);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 用户登陆后更改密码或给用户重置密码
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="PassWord"></param>
        /// <returns></returns>
        public static bool EditPwd(int UserId, string PassWord)
        {            
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_EditPwd", UserId, PassWord);
            DB.ExecuteNonQuery(dbCommand);
            return true;
        }

        /// <summary>
        /// 更改用户状态
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="State">0表示启用,1表示禁用</param>
        /// <returns></returns>
        public static bool ChangeState(int UserId, int State)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_ChangeState", UserId, State);
            DB.ExecuteNonQuery(dbCommand);
            return true;
        }

        public static void UpdateLastLogin(int UserId, string lastIp)
        { 
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("Ouser_UpdateLastLogin", UserId, lastIp);
            DB.ExecuteNonQuery(dbCommand);           
        }
        /// <summary>
        /// 更新个性化设置
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="MySetting"></param>
        /// <returns></returns>
        public static bool ChangeMySetting(int UserId, string MySetting)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_ChangeMySetting", UserId, MySetting);
            DB.ExecuteNonQuery(dbCommand);
            return true;
        }

        /// <summary>
        /// 根据部门获取部门下所有的员工，只获取状态为可用的员工.@Department 格式为 '''营业一部'',''风险部'''
        /// </summary>
        /// <param name="Departments"></param>
        /// <returns></returns>
        public static DataTable GetUserByDepartment(string Departments)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OUser_GetUserByDepartment", Departments);
            return  DB.ExecuteDataSet(dbCommand).Tables[0];
        }


        #region 一个帐号对应多个公司名,主要用于经销商名称变更
        public static int OUserMutlCmp_Add(int UserId, string AgentName, string Remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUserMutlCmp_Add");
            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, UserId);
            DB.AddInParameter(dbCommand, "AgentName", DbType.StringFixedLength, AgentName);
            DB.AddInParameter(dbCommand, "Remark", DbType.StringFixedLength, Remark);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool OUserMutlCmp_Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUserMutlCmp_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static DataTable OUserMutlCmp_GetListByUserId(int UserId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUserMutlCmp_GetListByUserId", UserId);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        public static bool OUserMutlCmp_Update(int ID, string AgentName, string Remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OUserMutlCmp_Update");
            DB.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            DB.AddInParameter(dbCommand, "AgentName", DbType.StringFixedLength, AgentName);
            DB.AddInParameter(dbCommand, "Remark", DbType.StringFixedLength, Remark);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        #endregion
    }
}
